package com.example.itemtouchhelper

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.SimpleCallback
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.itemtouchhelper.entity.Bean
import com.example.itemtouchhelper.utils.ColorUtils


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)
        val dataList = generateDataList(100)
        val recyclerViewAdapter = RecyclerViewAdapter(dataList, this)
        recyclerView.adapter = recyclerViewAdapter
//        recyclerView.addItemDecoration(LinearLayoutItemDecoration())
        val itemTouchHelper = ItemTouchHelper(CustomItemTouchCallback(recyclerViewAdapter, this))
        itemTouchHelper.attachToRecyclerView(recyclerView)
    }

    private fun generateDataList(count: Int): MutableList<Bean> {
        val dataList =  mutableListOf<Bean>()
        for (i in 0 until count) {
            val bean = Bean("position = $i", Color.parseColor(ColorUtils.generateRandomColor()))
            dataList.add(bean)
        }
        return dataList
    }

    /***
     * 创建右上角菜单
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.cardActivity ->  {
                val intent = Intent(this, CardActivity::class.java)
                startActivity(intent)
            }
        }
        return true
    }
}
